<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="../templates/principal.xhtml">

    <ui:define name="head">
        <script type="text/javascript">
            var intervalo = window.setTimeout(handleDiagnosticoRequest, 3000);
            function handleDiagnosticoRequest(xhr, status, args) {
                if (!args.loggedIn) {
                    PF('diagnosticoDialog').show();
                } else {
                    PF('diagnosticoDialog').hide();
                }

            }
        </script>
    </ui:define>

    <ui:define name="content" id="content">
        <h:form id="frmDiagnostico">
            <p:panel id="panel" header="Bem Vindo ao Cadastro de Diagnostico">
                <p:growl id="messages" life="5000" />
                
                <p:focus context="inDescricaoDiagnostico"/>

                <p:dataTable id="tabDiagnostico" widgetVar="wtabDiagnostico" var="diagnostico" value="#{diagnosticoBean.lazyModel}" 
                             lazy="true" paginator="true" rows="12" paginatorPosition="top"
                             emptyMessage="Nenhum Registro Retornado ..." filterEvent="enter">

                    <p:column headerText="Nome Diagnóstico" sortBy="#{diagnostico.dscDiagnostico}" filterBy="#{diagnostico.dscDiagnostico}">
                        <div style="text-align: left;">
                            <h:outputText value="#{diagnostico.dscDiagnostico}" />
                        </div>
                    </p:column>

                    <p:column style="width: 100px;">
                        <h:panelGrid columns="4" styleClass="actions" cellpadding="0">
                            <p:commandButton id="selectButtonDiagnostico" update=":frmDiagnostico:panel :frmDiagnostico:messages" 
                                             icon="ui-icon-search" title="Visualizar // Alterar Diagnostico" oncomplete="PF('diagnosticoDialog').show()">
                                <f:setPropertyActionListener value="#{diagnostico}" target="#{diagnosticoBean.selectedDiagnostico}" />
                                <f:setPropertyActionListener value="#{false}" target="#{diagnosticoBean.salvar}" />
                            </p:commandButton>

                            <p:commandButton id="excluirButtonDiagnostico" update=":frmDiagnostico:panel :frmDiagnostico:messages" 
                                             icon="ui-icon-trash" title="Excluir Diagnostico" oncomplete="PF('confirmation').show()">
                                <f:setPropertyActionListener value="#{diagnostico}" target="#{diagnosticoBean.selectedDiagnostico}" />
                            </p:commandButton>
                        </h:panelGrid>
                    </p:column>

                    <f:facet name="footer">
                        <div style="text-align: left;">
                            <p:commandButton id="incluirButtonBaco" actionListener="#{diagnosticoBean.preparaInserir}" 
                                             update=":frmDiagnostico:panel :frmDiagnostico:messages" value="Novo Diagnostico"
                                             icon="ui-icon-pencil" title="Cadastrar Novo Diagnostico" oncomplete="PF('diagnosticoDialog').show()" />
                        </div>
                    </f:facet>
                </p:dataTable>

                <p:dialog id="dialog" header="Detalhes Diagnostico" widgetVar="diagnosticoDialog" resizable="true" maximizable="true"
                          width="1500" showEffect="explode" hideEffect="explode" position="top">

                    <p:outputPanel id="outDiagnostico">
                        <p:panelGrid id="panelGrid" style="width: 100%; text-align: left;">
                            <p:row>
                                <p:column style="width: 20%;">
                                    <h:outputLabel for="inDescricaoDiagnostico" value="Nome do Diagnóstico:" />
                                </p:column>
                                <p:column>
                                    <p:inputText id="inDescricaoDiagnostico" value="#{diagnosticoBean.selectedDiagnostico.dscDiagnostico}" style="width: 100%; font-weight:bold; " maxlength="100" />
                                </p:column>
                            </p:row>
                            <f:facet name="footer">
                                <p:row>
                                    <p:column colspan="2">
                                        <p:dataTable id="tabDiagnostico1" widgetVar="wtabDiagnostico1" var="diagnostico1" value="#{diagnosticoBean.selectedDiagnostico.diagnosticoNivel1List}" 
                                                     paginator="true" rows="10" paginatorPosition="top" rowIndexVar="rowid"
                                                     emptyMessage="Nenhum Registro Retornado ..." filterEvent="enter">

                                            <p:column headerText="Descrição" sortBy="#{diagnostico1.dscDiagnosticoNivel1}" filterBy="#{diagnostico1.dscDiagnosticoNivel1}">
                                                <div style="text-align: left;">
                                                    <h:outputText value="#{diagnostico1.dscDiagnosticoNivel1}" />
                                                </div>
                                            </p:column>

                                            

                                            <p:column style="width: 100px;">
                                                <h:panelGrid columns="4" styleClass="actions" cellpadding="0">
                                                    <p:commandButton id="selectButtonDiagnostico1" update=":frmDiagnostico:outDiagnostico :frmDiagnostico:messages" 
                                                                     icon="ui-icon-search" title="Visualizar // Alterar Descrição">
                                                        <f:setPropertyActionListener value="#{diagnostico1}" target="#{diagnosticoBean.selectedDiagnosticoNivel1}" />
                                                        <f:setPropertyActionListener value="#{false}" target="#{diagnosticoBean.altera}" />
                                                        <f:setPropertyActionListener value="#{rowid}" target="#{diagnosticoBean.rowid}" />
                                                    </p:commandButton>

                                                    <p:commandButton id="excluirButtonDiagnostico1" update=":frmDiagnostico:outDiagnostico :frmDiagnostico:messages" 
                                                                     icon="ui-icon-trash" title="Excluir Descrição" oncomplete="PF('confirmationDescricao').show()">
                                                        <f:setPropertyActionListener value="#{diagnostico1}" target="#{diagnosticoBean.selectedDiagnosticoNivel1}" />
                                                        <f:setPropertyActionListener value="#{rowid}" target="#{diagnosticoBean.rowid}" />
                                                    </p:commandButton>
                                                </h:panelGrid>
                                            </p:column>

                                            <f:facet name="footer">
                                                <div style="text-align: left;">
                                                    <p:commandButton id="incluirButton1" actionListener="#{diagnosticoBean.preparaInserirDescricao}" 
                                                                     update=":frmDiagnostico:outDiagnostico :frmDiagnostico:messages" value="Nova Descrição"
                                                                     icon="ui-icon-pencil" title="Cadastrar Nova Descrição" />
                                                </div>
                                            </f:facet>
                                        </p:dataTable>
                                    </p:column>
                                </p:row>

                                <p:row>
                                    <p:column colspan="2">
                                        <p:panelGrid id="panelGridDescricao" style="width: 100%; text-align: center;">
                                            <f:facet name="header">
                                                <p:row>
                                                    <p:column>
                                                        <h:outputText value="Descrição" />
                                                    </p:column>

                                                    
                                                </p:row>
                                            </f:facet>

                                            <p:row>
                                                <p:column>
                                                    <div style="text-align: left;">
                                                        <p:inputText value="#{diagnosticoBean.selectedDiagnosticoNivel1.dscDiagnosticoNivel1}" maxlength="200" style="width: 98%; font-weight:bold; " />
                                                    </div>
                                                </p:column>

                                                
                                            </p:row>
                                            <f:facet name="footer">
                                                <p:row>
                                                    <p:column colspan="6">
                                                        <div style="text-align: right;">
                                                            <p:commandButton icon="ui-icon-disk" value="Salvar Descriçao" 
                                                                             actionListener="#{diagnosticoBean.inserirDescricao}" rendered="#{diagnosticoBean.altera}" 
                                                                             update=":frmDiagnostico:outDiagnostico :frmDiagnostico:messages" />
                                                            <p:commandButton icon="ui-icon-disk" value="Salvar Descriçao"
                                                                             actionListener="#{diagnosticoBean.alterarDescricao}" rendered="#{!diagnosticoBean.altera}"
                                                                             update=":frmDiagnostico:outDiagnostico :frmDiagnostico:messages" />
                                                        </div>
                                                    </p:column>
                                                </p:row>
                                            </f:facet>
                                        </p:panelGrid>
                                    </p:column>
                                </p:row>
                            </f:facet>
                        </p:panelGrid>
                        <div style="text-align: left;">
                            <p:commandButton icon="ui-icon-pencil" value="Salvar Diagnóstico" oncomplete="handleDiagnosticoRequest(xhr, status, args)" 
                                             actionListener="#{diagnosticoBean.inserir}" rendered="#{diagnosticoBean.salvar}" 
                                             update=":frmDiagnostico:panel :frmDiagnostico:messages" />
                            <p:commandButton icon="ui-icon-document" value="Salvar Diagnóstico" oncomplete="handleDiagnosticoRequest(xhr, status, args)"
                                             actionListener="#{diagnosticoBean.alterar}" rendered="#{!diagnosticoBean.salvar}"
                                             update=":frmDiagnostico:panel :frmDiagnostico:messages" />
                        </div>
                    </p:outputPanel>
                </p:dialog>

                <p:dialog header="Confirma a Exclusão desse Diagnostico?" widgetVar="confirmation">

                    <p:commandButton value="Sim" update=":frmDiagnostico:panel :frmDiagnostico:messages" oncomplete="PF('confirmation').hide(), PF('diagnosticoDialog').hide()"
                                     actionListener="#{diagnosticoBean.excluir}" />
                    <p:commandButton value="Não" onclick="PF('confirmation').hide();" type="button" />

                </p:dialog>

                <p:dialog header="Confirma a Exclusão dessa Descrição?" widgetVar="confirmationDescricao">

                    <p:commandButton value="Sim" update=":frmDiagnostico:outDiagnostico :frmDiagnostico:messages" oncomplete="PF('confirmationDescricao').hide()"
                                     actionListener="#{diagnosticoBean.excluirDescricao}" />
                    <p:commandButton value="Não" onclick="PF('confirmationDescricao').hide();" type="button" />

                </p:dialog>
            </p:panel>
        </h:form>
    </ui:define>
</ui:composition>